package development.app.mxh;

import iplayparticle.view.iplayparticleResources;

import java.util.Vector;

import particle.core.platform.IPlatformPictureImportEventHandler;
import particle.core.platform.Platform;
import particle.core.platform.PlatformPictureImportEvent;
import particle.core.tools.ByteBuffer;
import particle.core.view.assets.Image;
import state.Clip;
import state.DrawListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.TouchScreenListener;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.component.GameScrollBar;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.socket.DataPackage;
import state.socket.MapDataPackage;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.NumberUtil;
import state.util.array.ByteArray;
import state.util.array.StringArray;
import development.Constants;
import development.DrawUtils;
import development.Global;
import development.group.FrmGroupKetBanMangXHChat;
import development.socket.SocketClientUtil;

public class FrmUserGroup extends GameForm implements EventListener, TouchScreenListener, IPlatformPictureImportEventHandler {

  public static final int CREATE_USER_GROUP_REQUEST = 720;

  public static final int LIST_USER_GROUP_REQUEST = 721;
  public static final int LIST_USER_GROUP_RESPONSE = 722;

  public static final int CHANGE_AVATAR_REQUEST = 723;
  public static final int CHANGE_AVATAR_RESPONSE = 724;

  public static final int TRANSFER_MONEY_REQUEST = 725;
  public static final int TRANSFER_MONEY_RESPONSE = 726;

  public static final int FEED_BACK_REQUEST = 727;
  public static final int FEED_BACK_RESPONSE = 728;

  public static final int LEAVE_USER_GROUP_REQUEST = 729;
  public static final int LEAVE_USER_GROUP_RESPONSE = 730;

  public static final int JOIN_USER_GROUP_REQUEST = 731;
  public static final int JOIN_USER_GROUP_RESPONSE = 732;

  public static final int VIEW_AVATAR_REQUEST = 733;
  public static final int VIEW_AVATAR_RESPONSE = 734;

  public static final int LIST_MEMBER_REQUEST = 735;
  public static final int LIST_MEMBER_RESPONSE = 736;

  public static final int EDIT_USER_GROUP_REQUEST = 737;
  public static final int EDIT_USER_GROUP_RESPONSE = 738;

  private static GameForm singleton;
  private ImageText text8;
  private ImageText text11;
  private String type;
  private int dy;

  private final int DEFAULT_TEXT_X = 5;
  private GameScrollBar scrollBar;
  private Clip textView;

  private TextField txtFeedBack;
  private TextField txtTransferMoney;
  private TextField txtName;
  private TextField txtSlogal;
  private TextField txtFee;
  private Sprite createButtonSprite;
  private GameImage buttonImage;
  private GameImage userGroupAvatar;
  private GameImage defaultAvatarFocused;
  private GameImage avatarFocused;
  private GameMenu menu;
  private GameMenu menuMember;

  private int packageNumber;
  private int totalPackage;
  private int percent;

  private int userGroupStart;
  private int userGroupEnd;

  private int createUserGroupIndex;
  private int editUserGroupIndex;

  private Hashtable _parameters;

  private Vector userGroups;
  private int userGroupIndex;
  private String strIntroduction;
  private String strLevel;

  private boolean isShowTransferMoney;
  private boolean isShowFeedback;

  private GameImage viewImage;

  private GameImage[] arrowImage;

  private int VIEW_COMMENT_IMAGE_DY;
  private int VIEW_COMMENT_IMAGE_DX;

  private Vector members;
  private int memberIndex;
  private GameImage onlineImage;
  private GameImage offlineImage;

  private int MEMBER_DISTANCE_HEIGHT;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmUserGroup();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    this._parameters = parameters;

    strIntroduction = "Bang hội là một diễn đàn để các game thủ cùng sở thích có thể giao lưu, tán ngẫu với nhau. Mỗi bang có 1 bang chủ,"
        + "có quyền mời các thành viên khác tham gia hoặc xóa các thành viên khỏi bang. Nghiêm cấm các nội dung có tính chất đồ trụy hoặc liên quan đến chính trị"
        + ". Nếu vi phạm admin Winplay có quyền xóa mà không cần báo trước";

    strLevel = "WinPlay xin thông báo, để tăng tính cạnh tranh giữa các Bang hội WinPlay sẽ tính CẤP ĐỘ cho mỗi bang. Dựa vào quỹ bang, số lượng thành viên, số lượng bài viết và tổng thời gian online."
        + " WinPlay sẽ tính ĐIỂM cho mỗi bang. Công thức tính điểm cho bang như sau: ĐIỂM = (Quỹ bang + Tổng thời gian online(giây) * 0.6 + số lượng bài viết * 10 + Số lượng thành viên * 30) / 100. Căn cứ vào số điểm"
        + "WinPlay sẽ trao huy chương cho các bang: đồng, bạc, vàng...";

    userGroups = new Vector();
    memberIndex = 0;
    userGroupIndex = 0;
    members = new Vector();
    userGroupStart = 0;
    userGroupEnd = 10;
    type = "list_user_group";
    dy = DrawUtils.getTopBarHeight();
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    text11 = FontManager.getFont(FontManager.FONT_SIZE_11);
    textView = new Clip(DEFAULT_TEXT_X, dy, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - dy - 10);
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);
    txtName = new TextField(_manager, TextField.CONSTRAINT_ANY, null);
    txtName.setAnchor(GameConstants.BOTTOM_LEFT_ANCHOR);
    txtName.setVisible(true);
    txtName.setFocused(true);

    txtSlogal = new TextField(_manager, TextField.CONSTRAINT_ANY, null);
    txtSlogal.setAnchor(GameConstants.BOTTOM_LEFT_ANCHOR);
    txtSlogal.setVisible(true);

    txtFee = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtFee.setAnchor(GameConstants.BOTTOM_LEFT_ANCHOR);
    txtFee.setVisible(true);

    txtTransferMoney = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, null);
    txtTransferMoney.setSize(GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X * 2, text8.getHeight());
    txtTransferMoney.setAnchor(GameConstants.BOTTOM_LEFT_ANCHOR);
    txtTransferMoney.setPosition(DEFAULT_TEXT_X, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 5);
    txtTransferMoney.setVisible(false);

    txtFeedBack = new TextField(_manager, TextField.CONSTRAINT_ANY, null);
    txtFeedBack.setSize(GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X * 2, text8.getHeight());
    txtFeedBack.setAnchor(GameConstants.BOTTOM_LEFT_ANCHOR);
    txtFeedBack.setPosition(DEFAULT_TEXT_X, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 5);
    txtFeedBack.setVisible(false);

    userGroupAvatar = Global.getDefaultAvatar();
    defaultAvatarFocused = ImageUtil.makeBrighter(userGroupAvatar, 2);
    buttonImage = new GameImage(iplayparticleResources.image_button, null, null);
    createButtonSprite = new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR);
    createButtonSprite.setImage(buttonImage);
    createButtonSprite.setAnchor(Constants.TOP_LEFT_ANCHOR);
    createButtonSprite.setTouchScreenListener(this);
    createButtonSprite.setVisible(false);
    createButtonSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
      }
    });

    if (Global.currentUser.getGroupId() != 0) {
      menu = new GameMenu(null, 
          new String[] { "Tường bang", "Rời bang", "Thành viên", "Tài khoản BC", "Xem ảnh", "Tạo bang", "Bang của bạn", "Giới thiệu", "Cách tính level", "Góp ý", "Chuyển tiền", "Menu MXH" }, 12, this,
          Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    } else {
      menu = new GameMenu(null, new String[] { "Tường bang", "Nhập bang", "Thành viên", "Tài khoản BC", "Xem ảnh", "Tạo bang", "Bang của bạn", "Giới thiệu", "Cách tính level", "Góp ý", "Chuyển tiền",
          "Menu MXH" }, 12, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    }
    if (Global.currentUser.isGroupMaster()) {
      menuMember = new GameMenu(null, new String[] { "Sửa Bang", "Xóa Bang", "Đuổi", "Tài khoản", "Kết bạn", "Chuyển tiền" }, 6, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    } else {
      menuMember = new GameMenu(null, new String[] { "Tài khoản", "Kết bạn", "Chuyển tiền" }, 3, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45);
    }
    BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
    bottomBar.setLabels("Menu", "Chọn", "");
    bottomBar.setLeftMenu("Menu", menu);
    _manager.enableTouchPad();

    isShowTransferMoney = false;
    isShowFeedback = false;

    VIEW_COMMENT_IMAGE_DX = 0;
    VIEW_COMMENT_IMAGE_DY = dy;

    arrowImage = new GameImage[4];
    // trai
    arrowImage[0] = ImageUtil.getImage(Constants.MANGXH_CATEGORY, "arrow.png");
    // tren
    arrowImage[1] = ImageUtil.rotate(arrowImage[0], 90);
    // phai
    arrowImage[2] = ImageUtil.rotate(arrowImage[0], 180);
    // duoi
    arrowImage[3] = ImageUtil.rotate(arrowImage[0], 270);

    onlineImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "icon_online.png");
    offlineImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "icon_Nghi-ngoi.png");

    DataPackage dataPackage = DataPackage.createDataPackage1(LIST_USER_GROUP_REQUEST);
    dataPackage.putString("");
    dataPackage.putInt(userGroupStart);
    dataPackage.putInt(userGroupEnd);
    Global.appClient.write(dataPackage);

    new Sprite(null, _manager, 0, 0, Constants.TOP_LEFT_ANCHOR).setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        DrawUtils.drawTopBar(g);
        if (type.equals("create_user_group")) {
          text8.drawString2(g, "Tạo Bang", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
        }
        if (type.equals("instroduction")) {
          text8.drawString2(g, "Giới thiệu Bang", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
        }
        if (type.equals("level")) {
          text8.drawString2(g, "Cách tính level", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
        }
        if (type.equals("list_member")) {
          UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
          text8.drawString2(g, "Thành viên: " + userGroup.name, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
        }
        if (type.equals("edit_user_group")) {
          text8.drawString2(g, "Thay đổi thông tin Bang", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_HCENTER_ANCHOR);
        }
      }
    }).setSize(Constants.SCREEN_WIDTH, 25);
  }

  public void draw(Graphics g) {
    DrawUtils.drawAppBackground(g);
    if (type.equals("edit_user_group")) {
      drawEditUserGroup(g);
    }
    if (type.equals("list_member")) {
      drawListMemeber(g);
    }
    if (type.equals("list_user_group")) {
      drawUserGroup(g);
    }
    if (type.equals("create_user_group")) {
      drawCreateUserGroup(g);
    }
    if (type.equals("instroduction")) {
      int textY = dy + 25;
      text8.drawParagraph(g, strIntroduction, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, DEFAULT_TEXT_X, textY);
    }
    if (type.equals("level")) {
      int textY = dy + 25;
      text8.drawParagraph(g, strLevel, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH, DEFAULT_TEXT_X, textY);
    }
    if (type.equals("view_avatar")) {
      drawViewImage(g);
    }
    if (isShowTransferMoney) {
      drawTransferMoney(g);
    }
    if (isShowFeedback) {
      drawFeedBack(g);
    }
    DrawUtils.drawTopBar(g);
    drawMenu(g);
  }

  private void drawMenu(Graphics g) {
    if (type.equals("list_member")) {
      DrawUtils.getBottomBar(_manager).setLeftMenu("Menu", menuMember);
      DrawUtils.getBottomBar(_manager).setRightLabel("Trở về");
    }
    if (type.equals("instroduction") || type.equals("level") || type.equals("view_avatar")) {
      DrawUtils.getBottomBar(_manager).setLabels("", "", "Trở về");
    }
    if (type.equals("list_user_group")) {
      DrawUtils.getBottomBar(_manager).setLabels("Menu", "Chọn", "Trở về");
      DrawUtils.getBottomBar(_manager).setLeftMenu("Menu", menu);
    }
    if (isShowTransferMoney || isShowFeedback) {
      DrawUtils.getBottomBar(_manager).setLabels("Đóng", "Đồng ý", "Xóa");
    }
  }

  private void drawEditUserGroup(Graphics g) {
    int textY = DrawUtils.getTopBarHeight() + 20;
    UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
    text8.drawString2(g, "Slogal:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    txtSlogal.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    txtSlogal.setSize(GameConstants.SCREEN_WIDTH - 85, text8.getHeight());
    txtSlogal.setPosition(text8.stringWidth("Phí gia nhập:") + 5, textY);
    txtSlogal.setText(userGroup.slogal);
    textY = textY + 20;
    text8.drawString2(g, "Phí gia nhập:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    txtFee.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    txtFee.setSize(GameConstants.SCREEN_WIDTH - 85, text8.getHeight());
    txtFee.setPosition(text8.stringWidth("Phí gia nhập:") + 5, textY);
    textY = textY + 20;
    text8.drawString2(g, "Logo bang:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    if (editUserGroupIndex != 2) {
      g.drawImage(Global.currentUser.getAvatarGroup(), text8.stringWidth("Phí gia nhập:") + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
    } else {
      g.drawImage(avatarFocused, text8.stringWidth("Phí gia nhập:") + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
    }
    textY = textY + Global.getDefaultAvatar().getHeight() + 5;
    createButtonSprite.setPosition(DEFAULT_TEXT_X, textY);
  }

  private void drawListMemeber(Graphics g) {
    int textY = dy;
    int index = 0;
    for (int i = 0; i < members.size(); i++) {
      Member member = (Member) members.elementAt(i);
      if (memberIndex == index) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, Global.getDefaultAvatar().getHeight());
      }
      GameImage avatar = Global.getAvatar(member.username);
      g.drawImage(avatar, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
      text8.setBold(true);
      if (member.isMaster()) {
        text8.drawString3(g, member.username + "-BC", GameColor.YELLOW_CODE, avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      } else {
        text8.drawString3(g, member.username, GameColor.YELLOW_CODE, avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      }
      if (member.isOnline()) {
        g.drawImage(onlineImage, GameConstants.SCREEN_WIDTH - 10, textY, GameConstants.TOP_RIGHT_ANCHOR);
      } else {
        g.drawImage(offlineImage, GameConstants.SCREEN_WIDTH - 10, textY, GameConstants.TOP_RIGHT_ANCHOR);
      }
      text8.setBold(false);
      textY = textY + 20;
      text8.drawString3(g, "Điểm: " + member.score, GameColor.YELLOW_CODE, avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
      textY = textY + 30;
      g.setColor(0xC0C0C0);
      g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
      index++;
    }
  }

  private void drawViewImage(Graphics g) {
    g.drawImage(viewImage, VIEW_COMMENT_IMAGE_DX, VIEW_COMMENT_IMAGE_DY, GameConstants.TOP_LEFT_ANCHOR);
    g.drawImage(arrowImage[0], 0, GameConstants.SCREEN_HEIGHT / 2, GameConstants.TOP_LEFT_ANCHOR);
    g.drawImage(arrowImage[1], GameConstants.SCREEN_WIDTH / 2, 0, GameConstants.TOP_LEFT_ANCHOR);
    g.drawImage(arrowImage[2], GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT / 2, GameConstants.TOP_RIGHT_ANCHOR);
    g.drawImage(arrowImage[3], GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight(), GameConstants.BOTTOM_LEFT_ANCHOR);
  }

  private void drawFeedBack(Graphics g) {
    g.setColor(0xA9A9A9);
    g.fillRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
    g.drawRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
    text8.drawString2(g, "Nội dung góp ý:", GameColor.WHITE_CODE, 5, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 25, GameConstants.BOTTOM_LEFT_ANCHOR);
    txtFee.draw(g);
  }

  private void drawTransferMoney(Graphics g) {
    g.setColor(0xA9A9A9);
    g.fillRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
    g.drawRect(0, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 40);
    text8.drawString2(g, "Số tiền muốn chuyển:", GameColor.WHITE_CODE, 5, GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight() - 25, GameConstants.BOTTOM_LEFT_ANCHOR);
    txtTransferMoney.draw(g);
  }

  private int height = 0;

  private void drawUserGroup(Graphics g) {
    if (userGroupIndex == 0) {
      dy = DrawUtils.getTopBarHeight();
    }
    int textY = dy;
    for (int i = 0; i < userGroups.size(); i++) {
      UserGroup userGroup = (UserGroup) userGroups.elementAt(i);
      if (userGroupIndex == i) {
        g.setColor(0x53A4CF);
        g.fillRect(0, textY, GameConstants.SCREEN_WIDTH, height);
        g.drawImage(userGroup.avatar, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
        text11.setBold(true);
        text11.drawString3(g, userGroup.name, GameColor.YELLOW_CODE, userGroup.avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        text11.setBold(false);
        textY = textY + 20;
        text8.drawString2(g, "(" + userGroup.totalUser + " TV," + userGroup.totalScore + " Điểm)", GameColor.WHITE_CODE, userGroup.avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
        textY = textY + userGroup.avatar.getHeight();
        textY = text8.drawParagraph(g, userGroup.slogal, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X, DEFAULT_TEXT_X, textY);
        text11.setBold(true);
        text11.drawString3(g, "Bang chủ: " + userGroup.owner, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text11.drawString3(g, "Bang phí: " + userGroup.fee, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text11.drawString3(g, "Quỹ bang: " + userGroup.totalMoney, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text11.drawString3(g, "Online: " + userGroup.totalOnline, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text8.drawString3(g, userGroup.created, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
        text11.setBold(false);
        height = textY;
        textY = textY + 20;
        g.setColor(0xC0C0C0);
        g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
      } else {
        g.drawImage(userGroup.avatar, 0, textY, GameConstants.TOP_LEFT_ANCHOR);
        text11.setBold(true);
        text11.drawString3(g, userGroup.name, GameColor.YELLOW_CODE, userGroup.avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        text11.setBold(false);
        textY = textY + 20;
        text8.drawString2(g, "(" + userGroup.totalUser + " TV," + userGroup.totalScore + " Điểm)", GameColor.WHITE_CODE, userGroup.avatar.getWidth() + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
        textY = textY + userGroup.avatar.getHeight();
        textY = text8.drawParagraph(g, userGroup.slogal, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X, DEFAULT_TEXT_X, textY);
        text11.setBold(true);
        text11.drawString3(g, "Bang chủ: " + userGroup.owner, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text11.drawString3(g, "Bang phí: " + userGroup.fee, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text11.drawString3(g, "Quỹ bang: " + userGroup.totalMoney, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text11.drawString3(g, "Online: " + userGroup.totalOnline, GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR, scrollBar);
        textY = textY + 20;
        text8.drawString3(g, userGroup.created, GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH - DEFAULT_TEXT_X, textY, GameConstants.TOP_RIGHT_ANCHOR, scrollBar);
        text11.setBold(false);
        textY = textY + 20;
        g.setColor(0xC0C0C0);
        g.drawLine(0, textY, GameConstants.SCREEN_WIDTH, textY);
      }
    }
  }

  private void drawCreateUserGroup(Graphics g) {
    int textY = dy + 35;
    text8.drawString2(g, "Tên bang:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    txtName.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    txtName.setSize(GameConstants.SCREEN_WIDTH - 85, text8.getHeight());
    txtName.setPosition(text8.stringWidth("Phí gia nhập:") + 5, textY);
    textY = textY + 20;
    text8.drawString2(g, "Slogal:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    txtSlogal.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    txtSlogal.setSize(GameConstants.SCREEN_WIDTH - 85, text8.getHeight());
    txtSlogal.setPosition(text8.stringWidth("Phí gia nhập:") + 5, textY);
    textY = textY + 20;
    text8.drawString2(g, "Phí gia nhập:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    txtFee.setAnchor(GameConstants.TOP_LEFT_ANCHOR);
    txtFee.setSize(GameConstants.SCREEN_WIDTH - 85, text8.getHeight());
    txtFee.setPosition(text8.stringWidth("Phí gia nhập:") + 5, textY);
    txtFee.setText("0");
    textY = textY + 20;
    text8.drawString2(g, "Logo bang:", GameColor.YELLOW_CODE, DEFAULT_TEXT_X, textY, GameConstants.TOP_LEFT_ANCHOR);
    if (createUserGroupIndex != 3) {
      g.drawImage(userGroupAvatar, text8.stringWidth("Phí gia nhập:") + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
    } else {
      g.drawImage(defaultAvatarFocused, text8.stringWidth("Phí gia nhập:") + 5, textY, GameConstants.TOP_LEFT_ANCHOR);
    }
    textY = textY + Global.getDefaultAvatar().getHeight() + 5;
    createButtonSprite.setPosition(DEFAULT_TEXT_X, textY);
    // createButtonSprite.setVisible(true);
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }

  public void pointerPressed(Sprite source, int x, int y) {
  }

  public void pointerReleased(Sprite source, int x, int y) {
  }

  public void onRecieveData(DataPackage dataPackage) {
    int header = dataPackage.getHeader();
    MapDataPackage mapDataPackage = MapDataPackage.getMapPackage(dataPackage);
    type = mapDataPackage.popString("type");
    switch (header) {
    case EDIT_USER_GROUP_RESPONSE:
      Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Thay đổi thông tin", "Bang thành công" ));
      break;
    case LIST_MEMBER_RESPONSE:
      Global.systemCanvas.hideAlert();
      Member member = new Member();
      member.username = mapDataPackage.getString("user");
      member.score = mapDataPackage.getString("score");
      member.online = mapDataPackage.getInt("online");
      member.master = mapDataPackage.getInt("master");
      MEMBER_DISTANCE_HEIGHT = 50;
      members.addElement(member);
      break;
    case VIEW_AVATAR_RESPONSE:
      ByteArray btImage = mapDataPackage.getByteArray("image");
      viewImage = GameImage.createImage(btImage, 0, btImage.len());
      break;
    case JOIN_USER_GROUP_RESPONSE:
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn đã gia nhập bang thành công");
      break;
    case LEAVE_USER_GROUP_RESPONSE:
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Rời bang thành công");
      break;
    case TRANSFER_MONEY_RESPONSE:
      Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Bạn đã chuyển tiền cho Bang", "thành công" ));
      break;
    case FEED_BACK_RESPONSE:
      Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn đã góp ý thành công" );
      break;
    case LIST_USER_GROUP_RESPONSE:
      Global.systemCanvas.hideAlert();
      UserGroup userGroup = new UserGroup();
      userGroup.id = mapDataPackage.getString("id");
      userGroup.name = mapDataPackage.getString("name");
      userGroup.slogal = mapDataPackage.getString("slogal");
      userGroup.fee = mapDataPackage.getInt("fee");
      userGroup.totalMoney = mapDataPackage.getString("total_money");
      userGroup.totalScore = mapDataPackage.getString("total_score");
      userGroup.created = mapDataPackage.getString("created");
      userGroup.owner = mapDataPackage.getString("owner");
      btImage = mapDataPackage.getByteArray("avatar");
      userGroup.avatar = GameImage.createImage(btImage, 0, btImage.len());
      userGroup.avatar = ImageUtil.resizeImage2(userGroup.avatar, 50, 50);
      userGroup.totalRecord = mapDataPackage.getString("total_record");
      userGroup.totalUser = mapDataPackage.getString("total_user");
      userGroup.totalOnline = mapDataPackage.getString("total_user_online");
      packageNumber = mapDataPackage.getInt("packageNumber");
      totalPackage = mapDataPackage.getInt("totalPackage");
      userGroups.addElement(userGroup);
      break;
    case CHANGE_AVATAR_RESPONSE:
      ByteArray avatar = mapDataPackage.getByteArray("avatar");
      if (type.equals("create_user_group")) {
        userGroupAvatar = GameImage.createImage(avatar, 0, avatar.len());
        userGroupAvatar = ImageUtil.resizeImage2(userGroupAvatar, 50, 50);
        defaultAvatarFocused = ImageUtil.makeBrighter(userGroupAvatar, 2);
      } else if (type.equals("edit_user_group")) {
        userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
        userGroup.avatar = GameImage.createImage(avatar, 0, avatar.len());
        userGroup.avatar = ImageUtil.resizeImage2(userGroup.avatar, 50, 50);
        Global.currentUser.setAvatarGroup(userGroup.avatar);
        avatarFocused = ImageUtil.makeBrighter(userGroup.avatar, 2);
      }
      break;
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.LEFT:
      if (type.equals("view_avatar")) {
        VIEW_COMMENT_IMAGE_DX = VIEW_COMMENT_IMAGE_DX + 10;
      }
      break;
    case Key.RIGHT:
      if (type.equals("view_avatar")) {
        VIEW_COMMENT_IMAGE_DX = VIEW_COMMENT_IMAGE_DX - 10;
      }
      break;
    case Key.SOFT_LEFT:
      if (isShowTransferMoney) {
        isShowTransferMoney = false;
        txtTransferMoney.setVisible(false);
      }
      if (isShowFeedback) {
        isShowFeedback = false;
        txtFeedBack.setVisible(false);
      }
      break;
    case Key.SOFT_RIGHT:
      if (isShowTransferMoney) {
        txtTransferMoney.deleteLastChar();
        return;
      }
      if (isShowFeedback) {
        txtFeedBack.clearText();
        return;
      }
      if (type.equals("list_user_group")) {
        Global.nextState(FrmGroupKetBanMangXHChat.instance(), null);
      }
      if (type.equals("instroduction") || type.equals("level")) {
        type = "list_user_group";
      }
      if (type.equals("view_avatar")) {
        viewImage = null;
        type = "list_user_group";
      }
      if (type.equals("list_member")) {
        type = "list_user_group";
        members.removeAllElements();
        memberIndex = 0;
        userGroupIndex = 0;
        dy = DrawUtils.getTopBarHeight();
      }
      if (type.equals("edit_user_group")) {
        txtFee.setVisible(false);
        txtSlogal.setVisible(false);
        createButtonSprite.setVisible(false);
        type = "list_member";
      }
      break;
    case Key.DOWN:
      if (type.equals("list_member")) {
        if (memberIndex < members.size() - 1) {
          memberIndex++;
        }
      }
      if (type.equals("list_member")) {
        if (members.size() * (MEMBER_DISTANCE_HEIGHT) < GameConstants.SCREEN_HEIGHT - DrawUtils.getBottomBarHeight()) {
          return;
        }
        if (scrollBar.canScrollUp()) {
          dy = dy - (MEMBER_DISTANCE_HEIGHT);
        }
      }
      if (type.equals("view_avatar")) {
        VIEW_COMMENT_IMAGE_DY -= 10;
      }
      if (type.equals("list_user_group")) {
        if (userGroupIndex < userGroups.size() - 1) {
          userGroupIndex++;
          dy -= height;
        }
      }
      if (type.equals("edit_user_group")) {
        if (editUserGroupIndex < 3) {
          editUserGroupIndex++;
        }
        if (editUserGroupIndex == 0) {
          txtSlogal.setFocused(true);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (editUserGroupIndex == 1) {
          txtSlogal.setFocused(false);
          txtFee.setFocused(true);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (editUserGroupIndex == 2) {
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (editUserGroupIndex == 3) {
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
      }
      if (type.equals("create_user_group")) {
        if (createUserGroupIndex < 4) {
          createUserGroupIndex++;
        }
        if (createUserGroupIndex == 0) {
          txtName.setFocused(true);
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 1) {
          txtName.setFocused(false);
          txtSlogal.setFocused(true);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 2) {
          txtName.setFocused(false);
          txtSlogal.setFocused(false);
          txtFee.setFocused(true);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 3) {
          txtName.setFocused(false);
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 4) {
          txtName.setFocused(false);
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
      }
      break;
    case Key.UP:
      if (type.equals("list_member")) {
        if (memberIndex > 0) {
          memberIndex--;
        }
      }
      if (type.equals("list_member")) {
        if (scrollBar.canScrollDown()) {
          dy = dy + (MEMBER_DISTANCE_HEIGHT);
        }
      }
      if (type.equals("view_avatar")) {
        VIEW_COMMENT_IMAGE_DY += 10;
      }
      if (type.equals("list_user_group")) {
        if (userGroupIndex > 0) {
          userGroupIndex--;
          dy += height;
        }
      }
      if (type.equals("edit_user_group")) {
        if (editUserGroupIndex > 0) {
          editUserGroupIndex--;
        }
        if (editUserGroupIndex == 0) {
          txtSlogal.setFocused(true);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (editUserGroupIndex == 1) {
          txtSlogal.setFocused(false);
          txtFee.setFocused(true);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (editUserGroupIndex == 2) {
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (editUserGroupIndex == 3) {
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Thay đổi", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
      }
      if (type.equals("create_user_group")) {
        if (createUserGroupIndex > 0) {
          createUserGroupIndex--;
        }
        if (createUserGroupIndex == 0) {
          txtName.setFocused(true);
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 1) {
          txtName.setFocused(false);
          txtSlogal.setFocused(true);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 2) {
          txtName.setFocused(false);
          txtSlogal.setFocused(false);
          txtFee.setFocused(true);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 3) {
          txtName.setFocused(false);
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
        if (createUserGroupIndex == 4) {
          txtName.setFocused(false);
          txtSlogal.setFocused(false);
          txtFee.setFocused(false);
          createButtonSprite.setDrawListener(new DrawListener() {
            public void paint(Sprite source, Graphics g) {
              text8.drawString2(g, "Tạo bang", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
            }
          });
        }
      }
      break;
    case Key.FIRE:
      if (isShowFeedback) {
        String strFeedBack = txtFeedBack.getText();
        if (strFeedBack == null || strFeedBack.equals("")) {
          return;
        }
        UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
        DataPackage dataPackage = DataPackage.createDataPackage1(FEED_BACK_REQUEST);
        dataPackage.putString(userGroup.id);
        dataPackage.putString(strFeedBack);
        Global.appClient.write(dataPackage);
        Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
        isShowFeedback = false;
        txtFeedBack.setVisible(false);
        txtFeedBack.clearText();
      }
      if (isShowTransferMoney) {
        String strMoney = txtTransferMoney.getText();
        if (strMoney == null || strMoney.equals("")) {
          return;
        }
        int money = NumberUtil.parseInt(strMoney);
        if (money == NumberUtil.ERROR_VALUE) {
          Global.alert.showAlert(this, GameAlert.OK_TYPE, "Số $ phải là kiểu số");
          return;
        }
          
        if (money <= 0) {
          return;
        }
        if (money > Global.currentUser.getMoney()) {
          money = Global.currentUser.getMoney();
        }
        if (type.equals("list_user_group")) {
          UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
          DataPackage dataPackage = DataPackage.createDataPackage1(TRANSFER_MONEY_REQUEST);
          dataPackage.putString(userGroup.id);
          dataPackage.putInt(money);
          Global.appClient.write(dataPackage);
          Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
        } else if (type.equals("list_member")) {
          Member member = (Member) members.elementAt(memberIndex);
          SocketClientUtil.transferMoney(Global.currentUser.getName(), member.username, money);
        }
        isShowTransferMoney = false;
        txtTransferMoney.clearText();
        txtTransferMoney.setVisible(false);
      }
      if (type.equals("edit_user_group")) {
        if (editUserGroupIndex == 2) {
          Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1( "Bạn có muốn thay ảnh", "đại diện của bang?" ), 50);
          return;
        }
        if (editUserGroupIndex == 3) {
          if (txtSlogal.getText().length() == 0) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa nhập khẩu hiệu bang");
            return;
          }
        }
        String slogal = txtSlogal.getText();
        String fee = txtFee.getText();
        if (fee == null || fee.equals("")) {
          fee = "0";
        }
        
        int feeInt = NumberUtil.parseInt(fee);
        if (feeInt == NumberUtil.ERROR_VALUE) {
          Global.alert.showAlert(this, GameAlert.OK_TYPE, "Phí bang phải là kiểu số");
          return;
        }
        
        DataPackage dataPackage = DataPackage.createDataPackage1(EDIT_USER_GROUP_REQUEST);
        dataPackage.putString(String.valueOf(Global.currentUser.getGroupId()));
        dataPackage.putString(slogal);
        dataPackage.putString(fee);
        Global.appClient.write(dataPackage);
        Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
        txtSlogal.setVisible(false);
        txtFee.setVisible(false);
        createButtonSprite.setVisible(false);
      }
      if (type.equals("create_user_group")) {
        if (createUserGroupIndex == 3) {
          Global.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1( "Bạn có muốn thay ảnh", "đại diện của bang?" ), 50);
        }
        if (createUserGroupIndex == 4) {
          if (txtName.getText().length() == 0) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa nhập tên bang");
            return;
          }
          if (txtSlogal.getText().length() == 0) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Bạn chưa nhập khẩu hiệu bang");
            return;
          }
          String name = txtName.getText();
          String slogal = txtSlogal.getText();
          String fee = txtFee.getText();
          if (fee == null || fee.equals("")) {
            fee = "0";
          }
          
          int feeInt = NumberUtil.parseInt(fee);
          if (feeInt == NumberUtil.ERROR_VALUE) {
            Global.alert.showAlert(this, GameAlert.OK_TYPE, "Phí bang phải là kiểu số");
            return;
          }
          
          DataPackage dataPackage = DataPackage.createDataPackage1(CREATE_USER_GROUP_REQUEST);
          dataPackage.putString(name);
          dataPackage.putString(slogal);
          dataPackage.putString(fee);
          Global.appClient.write(dataPackage);
          Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
          userGroups.removeAllElements();
          txtName.setVisible(false);
          txtSlogal.setVisible(false);
          txtFee.setVisible(false);
          createButtonSprite.setVisible(false);
        }
      }
      if (type.equals("list_user_group")) {
        UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
        DataPackage dataPackage = DataPackage.createDataPackage1(LIST_MEMBER_REQUEST);
        dataPackage.putString(userGroup.id);
        Global.appClient.write(dataPackage);
        Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
        dy = DrawUtils.getTopBarHeight();
      }
      break;
    default:
      if (txtName.isFocused()) {
        txtName.keyReleased1(keyCode);
      }
      if (txtSlogal.isFocused()) {
        txtSlogal.keyReleased1(keyCode);
      }
      if (txtFee.isFocused()) {
        txtFee.keyReleased1(keyCode);
      }
      if (txtTransferMoney.isFocused()) {
        txtTransferMoney.keyReleased1(keyCode);
      }
      if (txtFeedBack.isFocused()) {
        txtFeedBack.keyReleased1(keyCode);
      }
      break;
    }
  }

  // TODO: đang chưa lấy được tên file ảnh khi import ảnh
  public void onPlatformPictureImport(Platform platform, PlatformPictureImportEvent event) {
    Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang up ảnh...", 99);
    
    platform.removePictureImportEventHandler(this);
    ByteBuffer byteBuffer = new ByteBuffer();
    event.picture.toBuffer(byteBuffer, Image.FormatPNG);
    ByteArray data = ByteArray.create(byteBuffer.getLength());
    for (int i = 0; i < data.len(); i++) {
      data.set(i, byteBuffer.getByte(i));
    }
    
    DataPackage dataPackage = DataPackage.createDataPackage3(CHANGE_AVATAR_REQUEST, data.len() + 150);
    dataPackage.putString(type);
    dataPackage.putString("Không tên");
    dataPackage.putByteArray(data);
    Global.appClient.write(dataPackage);
    
    Global.systemCanvas.hideAlert();
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 50 && eventType == GameAlert.YES_BUTTON) {
      Platform platform = Platform.getInstance();
      platform.addPictureImportEventHandler(this);
      platform.startPictureImport();
    }
  }

  public void onActionPerform(Event event) {
    // "Tường bang", "Nhập bang","Rời bang", "Thành viên", "Tài khoản BC", "Xem ảnh", "Tạo bang", "Bang của bạn", "Giới thiệu", "Cách tính level", "Góp ý", "Chuyển tiền",
    // "Menu MXH"
    String action = event.getAction();
    if (action.equals("Menu MXH")) {
      FrmGroupKetBanMangXHChat.tabIndex = 1;
      Global.nextState(FrmGroupKetBanMangXHChat.instance(), _parameters);
    } else if (action.equals("Tạo bang")) {
      type = "create_user_group";
      txtName.setVisible(true);
      txtSlogal.setVisible(true);
      txtFee.setVisible(true);
      createButtonSprite.setVisible(true);
    } else if (action.equals("Giới thiệu")) {
      type = "instroduction";
    } else if (action.equals("Cách tính level")) {
      type = "level";
    } else if (action.equals("Bang của bạn")) {
      DataPackage dataPackage = DataPackage.createDataPackage1(LIST_USER_GROUP_REQUEST);
      dataPackage.putString(Global.currentUser.getName());
      dataPackage.putInt(userGroupStart);
      dataPackage.putInt(userGroupEnd);
      Global.appClient.write(dataPackage);
      Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
      userGroups.removeAllElements();
      userGroupIndex = 0;
    } else if (action.equals("Chuyển tiền")) {
      isShowTransferMoney = true;
      txtTransferMoney.setVisible(true);
      txtTransferMoney.setFocused(true);
    } else if (action.equals("Góp ý")) {
      isShowFeedback = true;
      txtFeedBack.setVisible(true);
      txtFeedBack.setFocused(true);
    } else if (action.equals("Rời bang")) {
      DataPackage dataPackage = DataPackage.createDataPackage1(LEAVE_USER_GROUP_REQUEST);
      Global.appClient.write(dataPackage);
      Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
    } else if (action.equals("Nhập bang")) {
      UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
      DataPackage dataPackage = DataPackage.createDataPackage1(JOIN_USER_GROUP_REQUEST);
      dataPackage.putString(userGroup.id);
      Global.appClient.write(dataPackage);
      Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
    } else if (action.equals("Xem ảnh")) {
      UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
      DataPackage dataPackage = DataPackage.createDataPackage1(VIEW_AVATAR_REQUEST);
      dataPackage.putString(userGroup.owner);
      Global.appClient.write(dataPackage);
      Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
    } else if (action.equals("Thành viên")) {
      UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
      DataPackage dataPackage = DataPackage.createDataPackage1(LIST_MEMBER_REQUEST);
      dataPackage.putString(userGroup.id);
      Global.appClient.write(dataPackage);
      Global.alert.showAlert2(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang tải...", 99).setAlertTimeOut(5000);
      dy = DrawUtils.getTopBarHeight();
    } else if (action.equals("Kết bạn")) {
      Member member = (Member) members.elementAt(memberIndex);
      if (!member.username.equals(Global.currentUser.getName())) {
        SocketClientUtil.addFriendRequest(member.username);
      }
    } else if (action.equals("Sửa Bang")) {
      UserGroup userGroup = (UserGroup) userGroups.elementAt(userGroupIndex);
      avatarFocused = ImageUtil.makeBrighter(userGroup.avatar, 2);
      type = "edit_user_group";
      createButtonSprite.setVisible(true);
      txtSlogal.setFocused(true);
      txtSlogal.setVisible(true);
      txtFee.setVisible(true);
    }else if(action.equals("Tài khoản")){
      Member member = (Member) members.elementAt(memberIndex);
      Hashtable parameters = new Hashtable();
      parameters.put("User", member.username);
      FrmAccount.backForm = this;
      Global.nextState(FrmAccount.instance(), parameters);
    }else if (action.equals("Đuổi")) {
    }
  }

  public void destroy() {
    text8 = null;
    text11 = null;
    type = null;
    scrollBar = null;
    textView = null;

    txtFeedBack = null;
    txtTransferMoney = null;
    txtName = null;
    txtSlogal = null;
    txtFee = null;
    createButtonSprite = null;
    buttonImage = null;
    userGroupAvatar = null;
    defaultAvatarFocused = null;
    menu = null;

    _parameters = null;

    userGroups = null;
    strIntroduction = null;
    strLevel = null;
    isShowTransferMoney = false;
    isShowFeedback = false;
    viewImage = null;
    arrowImage = null;

    members = null;

    onlineImage = null;
    offlineImage = null;

    avatarFocused = null;
    dy = DrawUtils.getTopBarHeight();
  }

  private class UserGroup {
    public String id;
    public String name;
    public String slogal;
    public int fee;
    public GameImage avatar;
    public int status;
    public String totalMoney;
    public String totalScore;
    public String owner;
    public String created;
    public String totalRecord;
    public String totalUser;
    public String totalOnline;
  }

  private class Member {
    String username;
    String score;
    int online;
    int master;

    public boolean isOnline() {
      return online == 1;
    }

    public boolean isMaster() {
      return master == 1;
    }
  }
}
